package com.lx.service.impl;

import com.lx.api.response.RelateItemInfo;
import com.lx.api.response.TypeInfo;
import com.lx.dao.*;
import com.lx.model.*;
import com.lx.service.SettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class SettingServiceImpl implements SettingService{

    @Autowired
    private PrintSystemParamMapper systemParamMapper;

    @Autowired
    private PrintKitchenFormatMapper kitchenFormatMapper;

    @Autowired
    private PrintBillFormatMapper billFormatMapper;

    @Autowired
    private PrintPlanMapper printPlanMapper;
    @Autowired
    private PrintDishTypeMapper printDishTypeMapper;
    @Autowired
    private PrintShopDishMapper printShopDishMapper;
    @Autowired
    private PrintDishPlanMapper printDishPlanMapper;
    @Autowired
    private PrintTableTypeMapper printTableTypeMapper;
    @Autowired
    private PrintTableInfoMapper printTableInfoMapper;
    @Autowired
    private PrintTablePlanMapper printTablePlanMapper;


    @Override
    public PrintSystemParam getSystemParam(Long shopId) {
        return systemParamMapper.selectByShopId(shopId);
    }

    @Override
    public void saveSystemParam(PrintSystemParam param) {
        param.setUpdateTime(new Date());
        if(param.getId()==null){//新增
            param.setCreateTime(new Date());
            systemParamMapper.insert(param);
        }else{
            systemParamMapper.updateByPrimaryKey(param);
        }
    }

    @Override
    public PrintKitchenFormat getPrintKitchenFormat(Long shopId) {
        return kitchenFormatMapper.selectByShopId(shopId);
    }

    @Override
    public void savePrintKitchenFormat(PrintKitchenFormat format) {
        format.setUpdateTime(new Date());
        if(format.getId()==null){//新增
            format.setCreateTime(new Date());
            kitchenFormatMapper.insert(format);
        }else{
            kitchenFormatMapper.updateByPrimaryKey(format);
        }
    }

    @Override
    public PrintBillFormat getPrintBillFormat(Long shopId) {
        return billFormatMapper.selectByShopId(shopId);
    }

    @Override
    public void savePrintBillFormat(PrintBillFormat format) {
        format.setUpdateTime(new Date());
        if(format.getId()==null){//新增
            format.setCreateTime(new Date());
            billFormatMapper.insert(format);
        }else{
            billFormatMapper.updateByPrimaryKey(format);
        }

    }

    @Override
    public List<PrintPlan> findPrintPlan(Long shopId) {
        return printPlanMapper.selectByShopId(shopId);
    }

    @Override
    public Long savePrintPlan(PrintPlan plan) {
        plan.setUpdateTime(new Date());
        if(plan.getId()==null){
            plan.setCreateTime(new Date());
            printPlanMapper.insert(plan);
        }else {
            printPlanMapper.updateByPrimaryKey(plan);
        }
        PrintPlan p = printPlanMapper.selectByPlanNo(plan.getShopId(),plan.getPlanNo());
        if(p!=null)return p.getId();
        return plan.getId();
    }

    @Override
    public void deletePrintPlanById(Long printPlanId) {
        printPlanMapper.deleteByPrimaryKey(printPlanId);
    }

    @Override
    public List<TypeInfo> findDishType(Long merchantsId,Long shopId) {
        return printDishTypeMapper.selectByMerchantsId(merchantsId,shopId);
    }

    @Override
    public List<RelateItemInfo> findShopDishForRelate(Long merchantsId,Long shopId, Long dishTypeNo, String shopDishNo) {
        String name = StringUtils.isNotBlank(shopDishNo)?("%"+shopDishNo+"%"):null;
        return printShopDishMapper.findShopDishForRelate(merchantsId,shopId,dishTypeNo,name,shopDishNo);
    }

    @Override
    public void saveDishPrintPlans(List<PrintDishPlan> dishPlans) {
        if(dishPlans!=null&&dishPlans.size()>0){
            for(PrintDishPlan plan:dishPlans)saveDishPrintPlan(plan);
        }

    }

    @Override
    public List<TypeInfo> findTableType(Long shopId) {
        return printTableTypeMapper.selectByShopId(shopId);
    }

    @Override
    public List<RelateItemInfo> findShopTableForRelate(Long shopId, String tableTypeName, String tableNo) {
        String name = StringUtils.isNotBlank(tableNo)?("%"+tableNo+"%"):null;
        return printTableInfoMapper.findShopTableForRelate(shopId,tableTypeName,name,tableNo);
    }

    @Override
    public void saveTablePrintPlan(PrintTablePlan vo) {
        PrintTablePlan po = printTablePlanMapper.selectByTableId(vo.getTableId());
        if (po==null){
            vo.setCreateTime(new Date());
            vo.setUpdateTime(new Date());
            printTablePlanMapper.insert(vo);
        }else{
            po.setPlanIdString(vo.getPlanIdString());
            po.setUpdateTime(new Date());
            printTablePlanMapper.updateByPrimaryKey(po);
        }
    }

    @Override
    public void saveTablePrintPlans(List<PrintTablePlan> tablePlans) {
        if(tablePlans!=null&&tablePlans.size()>0){
            for(PrintTablePlan plan:tablePlans)saveTablePrintPlan(plan);
        }
    }

    @Override
    public void saveDishPrintPlan(PrintDishPlan vo) {
        PrintDishPlan po = printDishPlanMapper.selectByShopDishId(vo.getShopDishId());
        if (po==null){
            vo.setCreateTime(new Date());
            vo.setUpdateTime(new Date());
            printDishPlanMapper.insert(vo);
        }else{
            po.setPlanIdString(vo.getPlanIdString());
            po.setUpdateTime(new Date());
            printDishPlanMapper.updateByPrimaryKey(po);
        }


    }
}
